package cn.cloud.search.server.trans.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;

import cn.cloud.search.bean.config.Config;
import cn.cloud.search.bean.config.DomainConfig;
import cn.cloud.search.bean.config.FieldConfig;
import cn.cloud.search.bean.response.SearchHitItem;
import cn.cloud.search.server.trans.ITranslation;


public class DefaultTranslation implements ITranslation {

	protected static final Log logger=LogFactory.getLog(DefaultTranslation.class);
	
	private Config config;
	
	

	public Config getConfig() {
		return config;
	}



	public void setConfig(Config config) {
		this.config = config;
	}



	public SearchHitItem translation(Document doc) {
		// TODO Auto-generated method stub
		SearchHitItem shi = null;
		String type = doc.get("type");
		DomainConfig dc = config.getDomainByKey(type);
		if(null!=dc){
			shi = new SearchHitItem();
			List<FieldConfig> field_config_list = dc.getField_config_list();
			for(FieldConfig fc:field_config_list){
				logger.info("fc is:"+fc.getName()+"-"+fc.getIsId()+"-"+fc.getIsTitle()+"-"+fc.getIsContent());
				String value = doc.get(fc.getName());
				if(FieldConfig.IS_ID==fc.getIsId()){
					shi.setPath(dc.getUrl_path().replace("{id}", value));
				}
				
				if(FieldConfig.IS_TITLE==fc.getIsTitle()){
					shi.setTitle(value);
				}
				
				if(FieldConfig.IS_CONTENT==fc.getIsContent()){
					if(fc.getShowLength()<value.length())
						value = value.substring(0,fc.getShowLength());
					shi.setRemark(value);
				}
				
			}
		}
		return shi;
	}

}
